##############################################################
#Figure C6: Distribution of Risk Aversion and Income in CCES 2014
##############################################################

data <- read.dta("./input/cces-subset.dta")

smalld <- data[,c("employed", "out_of_labour", "race_14", "risk", "dem14", "education", 
                  "female", "income", "income_quarters", "age")]

smalld$income <- as.factor(smalld$income)
smalld$income_quarters <- as.factor(smalld$income_quarters)
smalld$education <- as.factor(smalld$education)

data5 <- na.omit(smalld)

data5.2 <- data5 %>% 
  group_by(income_quarters,risk) %>% 
  summarise(count=n()) %>% 
  mutate(perc=count/sum(count))


data5.2$income_q2 = factor(data5.2$income_quarters, levels=c(1,2,3,4), 
                           labels =c('Income:Bottom 25%','Income:25-50%', 'Income:50-75%', "Income:Top 25%"))

ggplot(data5.2, aes(x=as.factor(risk), y=perc, group=as.factor(income_quarters), fill=as.factor(income_quarters))) +
  geom_bar(stat= "identity", position="dodge") + 
  scale_y_continuous(labels=percent) + 
  facet_grid(~income_q2) +
  ylab("Percentage") + 
  xlab ("Risk Aversion")  + 
  theme_bw() + 
  theme(panel.background = element_blank(),
        panel.grid.major = element_blank(),
        panel.grid.minor =   element_blank(),
        legend.position = "none") + 
  scale_x_discrete(breaks=c(0,1,2,3),labels= c("Taker", "", "", "Averse"))  +
  scale_fill_manual(values=c("grey60", "grey60", "grey60", "grey60", "grey60", "grey60" ,"grey60"))

ggsave("./figures/figc6.pdf")  
